package com.oocl.venus.service.impl;

import java.util.Iterator;
import java.util.List;

import com.oocl.venus.dao.DepartmentDao;
import com.oocl.venus.dao.PersonDao;
import com.oocl.venus.entity.Department;
import com.oocl.venus.entity.Person;
import com.oocl.venus.service.DepartmentService;
import com.oocl.venus.utils.BeanFactory;



public class DepartmentServiceImpl implements DepartmentService {
	
	private BeanFactory beanFactory;
	private DepartmentDao departmentDao;
	private PersonDao personDao;
	
	public DepartmentServiceImpl() {
		try {
			beanFactory = new BeanFactory();
			personDao = beanFactory.getInstance("personDao");
			departmentDao = beanFactory.getInstance("departmentDao");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	
	public String deptSer(String data) {
		String[] orders = data.split(" ");
		if (orders[0].equals("A-D")) {
			return add(orders[1]);
		}
		if (orders[0].equals("U-D")) {
			return update(orders[1]);
		}
		if (orders[0].equals("D-D")) {
			return delete(orders[1]);
		}
		if (orders[0].equals("L-DA")) {
			return findAll(null);
		}
		if (orders[0].equals("L-D")) {
			return find(orders[1]);
		}
		else{
			return "Invalid statement,execute fail";
		}
	}

	public String add(String data) {
		try {
			String[] keyValues = data.split(",");// split the dept properties by ","
			Department dept = new Department();
			for (int i = 0; i < keyValues.length; i++) {
				String[] currentData = keyValues[i].trim().split(":");

				if (currentData[0].equals("dname")) {
					dept.setDname(currentData[1]);
				}
				if (currentData[0].equals("did")) {
					int did = Integer.parseInt(currentData[1]);
					dept.setDid(did);
				}
				if (currentData[0].equals("city")) {
					dept.setCity(currentData[1]);
				}
			}
			departmentDao.addDepartment(dept);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return "Add fail";
		}

		return "Add successfully";
	}

	public String delete(String data) {
		String[] currentData = data.split(":");
		int did = Integer.parseInt(currentData[1]);
		if(departmentDao.loadDepartment(did)==null) return "Did does not exist,delete fail";
		departmentDao.deleteDepartment(did);
		List<Person> p = departmentDao.showDepartmentAll(did);
		Iterator<Person> p1 = p.iterator();
		while(p1.hasNext()){
			personDao.deletePerson(p1.next().getPid());
		}
		return "Delete successfully";
	}

	public String update(String data) {
		try {
			String[] keyValues = data.split(",");// split the dept properties by ","
			Department dept = new Department();
			for (int i = 0; i < keyValues.length; i++) {
				String[] currentData = keyValues[i].trim().split(":");
				if (currentData[0].equals("dname")) {
					dept.setDname(currentData[1]);
				}
				if (currentData[0].equals("did")) {
					int did = Integer.parseInt(currentData[1]);
					if(departmentDao.loadDepartment(did)==null) return "Did does not exist,update fail";
					dept.setDid(did);
				}
				if (currentData[0].equals("city")) {
					dept.setCity(currentData[1]);
				}
			}
			departmentDao.updateDepartment(dept);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return "Update fail";
		}
		return "Update successfully";
	}

	public String find(String data) {
		String[] currentData = data.split(":");
		int did = Integer.parseInt(currentData[1]);
		Department d = departmentDao.loadDepartment(did);
		if(d==null) return "Did does not exist, load fail";
		int deptId = d.getDid();
	    String result = "did:"+String.valueOf(deptId)+" dname:"+d.getDname()+" city:"+d.getCity()+"\r\n";
		return result;
	}

	public String findAll(String data) {
		List<Department> de = departmentDao.showAllDepartment();
		Iterator<Department> d =de.iterator();
		if(d==null) return "Nothing exists in dept,load fail";
		String result = "";
		while(d.hasNext()){
			Department dNext = d.next();
			int deptId = dNext.getDid();
			result += "did:"+String.valueOf(deptId)+" dname:"+dNext.getDname()+" city:"+dNext.getCity()+"\r\n";
		}
		return result;
	}

}
